
#include "UIImage.h"

#include <SDL2/SDL_opengl.h>

#include <ALittleClient/LogSystem/LogSystem.h>

namespace ALittle
{

UIImage::UIImage()
: m_visible(true)
, m_texture_id(0)
, m_tex_top(0), m_tex_bottom(1), m_tex_left(0), m_tex_right(1)
, m_top(0), m_bottom(0), m_left(0), m_right(0)
, m_red(1), m_green(1), m_blue(1), m_alpha(1)
{ }

UIImage::~UIImage()
{
}

void UIImage::SetVisible(bool visible)
{
	m_visible = visible;
}

void UIImage::SetX(float x)
{
	float width = m_right - m_left;

	m_left = x;
	m_right = m_left + width;
}

void UIImage::SetY(float y)
{
	float height = m_bottom - m_top;

	m_top = y;
	m_bottom = m_top + height;
}

void UIImage::SetWidth(float width)
{
	m_right = m_left + width;
}

void UIImage::SetHeight(float height)
{
	m_bottom = m_top + height;
}

void UIImage::SetRed(float red)
{
	m_red = red;
}

void UIImage::SetGreen(float green)
{
	m_green = green;
}

void UIImage::SetBlue(float blue)
{
	m_blue = blue;
}

void UIImage::SetAlpha(float  alpha)
{
	m_alpha = alpha;
}

void UIImage::Render()
{
	if (!m_visible) return;
	if (!m_texture_id) return;

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, m_texture_id);
	glBegin(GL_QUADS);
		glColor4f(m_red, m_green, m_blue, m_alpha);
		glTexCoord2f(m_tex_left,	m_tex_top);		glVertex3f(m_left,  m_top,		0.0f);
		glTexCoord2f(m_tex_right,	m_tex_top);		glVertex3f(m_right, m_top,		0.0f);
		glTexCoord2f(m_tex_right,	m_tex_bottom);	glVertex3f(m_right, m_bottom,	0.0f);
		glTexCoord2f(m_tex_left,	m_tex_bottom);	glVertex3f(m_left,  m_bottom,	0.0f);
	glEnd();
	glDisable(GL_TEXTURE_2D);
}

void UIImage::SetTexture(GLuint texture_id)
{
	m_texture_id = texture_id;
}

void UIImage::SetTextureCoord(float top, float bottom, float left, float right)
{
	m_tex_top = top;
	m_tex_bottom = bottom;
	m_tex_left = left;
	m_tex_right = right;
}

} // ALittle
